package adapters;

import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Locale;

import utils.FalhasLocais;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.ufba.square.registro_square.R;

import com.nostra13.universalimageloader.core.ImageLoader;

public class CustomAdapterLocais extends BaseAdapter {
	
	private Context context;
	private ArrayList<FalhasLocais> locais;
	private String lang = Locale.getDefault().getLanguage().toString();

	public CustomAdapterLocais(Context context, ArrayList<FalhasLocais> locais){
		this.context = context;
		this.locais = locais;
	}

	@Override
	public int getCount() {
		return locais.size();
	}

	@Override
	public Object getItem(int position) {
		return locais.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<FalhasLocais> getItems(){
		return locais;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		//if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater)
				context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	
			convertView = mInflater.inflate(R.layout.activity_cadastro_local_item, null);
		//}

		//ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
		//ImageView img = (ImageView) convertView.findViewById(R.id.image_local);
		TextView local = (TextView) convertView.findViewById(R.id.text_local);

		// show The Image
		ImageView imgview = (ImageView) convertView.findViewById(R.id.image_local);
		ImageLoader.getInstance().displayImage(locais.get(position).imagem,imgview);

		//new DownloadImageTask(img).execute(locais.get(position).imagem);
		//notifyDataSetChanged();

		local.setId(locais.get(position).id);
		if (lang.equalsIgnoreCase("en"))
			local.setText(locais.get(position).title_en);
		else
			local.setText(locais.get(position).title);

		return convertView;
	}


	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
	    ImageView bmImage;

	    public DownloadImageTask(ImageView bmImage) {
	        this.bmImage = bmImage;
	    }

	    protected Bitmap doInBackground(String... urls) {
	        String urldisplay = urls[0];
	        Bitmap mIcon11 = null;
	        try {
		        URL aURL = new URL(urldisplay);
	        	final URLConnection conn = aURL.openConnection();
	            conn.connect();
	            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
	            mIcon11 = BitmapFactory.decodeStream(bis);
	            bis.close();
	            //InputStream in = new java.net.URL(urldisplay).openStream();
	            //mIcon11 = BitmapFactory.decodeStream(in);
	        } catch (Exception e) {
	            Log.e("Error", e.getMessage());
	            e.printStackTrace();
	        }
	        return mIcon11;
	    }

	    protected void onPostExecute(Bitmap result) {
	        bmImage.setImageBitmap(result);
	    }
	}
}